﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//buddy
using Zeta;
using Zeta.Common;
using Zeta.CommonBot;
using Zeta.Common.Plugins;
using Zeta.Internals;
using Zeta.Internals.Actors;
using Zeta.Internals.Service;

using BossHog.ProfileBuilder;

namespace BossHog
{
    public partial class Toolkit : Form
    {
        public Toolkit()
        {
            InitializeComponent();
            reloadProfileXml.FormatXml();
        }

        public class ActorGridItem
        {
            public ActorGridItem() { }
            public string Name { get; set; }
            public string ActorSNO { get; set; }
            public string Position { get; set; }
            public string ActorType { get; set; }
            public string ActorClass { get; set; }
            public string ActorGuid { get; set; }
        }

        List<ActorGridItem> actorItems = null;
        void BindActors()
        {
            lock (this)
            {
                try
                {

                    if (null != actorItems)
                    {
                        if (actorItems.Count > 0)
                        {
                            actorItems.Clear();
                        }
                        actorItems = null;
                    }

                    actorItems = new List<ActorGridItem>();
                    IEnumerator<ACD> ie = ZetaDia.Actors.GetActorsOfType<ACD>().GetEnumerator();
                    while (ie.MoveNext())
                    {
                        if (null != ie.Current)
                        {
                            ActorGridItem i = new ActorGridItem();

                            i.Name = ie.Current.Name;
                            i.ActorSNO = ie.Current.ActorSNO.ToString();
                            i.Position = ie.Current.Position.ToString();
                            i.ActorType = ie.Current.ActorType.ToString();
                            i.ActorClass = ie.Current.ActorClass.ToString();
                            i.ActorGuid = ie.Current.ACDGuid.ToString();

                            actorItems.Add(i);
                        }
                    }

                    actorGrid.AutoGenerateColumns = false;
                    actorGrid.DataSource = null;
                    actorGrid.DataSource = actorItems;

                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Verbose, "[BossHog] Error: " + ex.Message);
                }
            }
        }

        bool IsValidXml(string xml)
        {
            bool ret = true;
            lock (this)
            {
                System.Xml.XmlTextReader r = null;
                System.IO.StringReader sr = null;
                try
                {
                    sr = new System.IO.StringReader(xml);
                    r = new System.Xml.XmlTextReader(sr);
                    while (r.Read()) { }
                }
                catch (Exception ex)
                {
                    Logging.Write("[BossHog] Error: " + ex.Message);
                    ret = false;
                }
                finally
                {
                    if (null != r)
                    {
                        r.Close();
                        r = null;
                    }

                    if (null != sr)
                    {
                        sr.Close();
                        sr = null;
                    }
                }
            }
            return ret;
        }

        public void PluginPulse() 
        { 
        
        }

        public void ReloadRestart()
        {
            ZetaDia.Service.Games.LeaveGame();
            System.Threading.Thread.Sleep(12000);
            BotMain.Stop();
            #region save tmp file
            string fp = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\BWTmp.xml";
            System.IO.Stream s = null;
            System.IO.StreamWriter w = null;

            try
            {
                try
                {
                    if (System.IO.File.Exists(fp))
                    {
                        System.IO.File.Delete(fp);
                    }
                }
                catch (Exception failed) { Logging.Write(LogLevel.Verbose, "[BossHog] Error: " + failed.Message); }

                s = System.IO.File.Open(fp, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None);
                w = new System.IO.StreamWriter(s);
                lock (this)
                {
                    w.Write(reloadProfileXml.Text);
                }

            }
            catch (Exception ex)
            {
                Logging.Write("[BossHog] Error: " + ex.Message);
            }
            finally
            {
                if (null != w)
                {
                    w.Close();
                    w.Dispose();
                    w = null;
                }

                if (null != s)
                {
                    s.Close();
                    s.Dispose();
                    s = null;
                }
            }
            #endregion
            Zeta.CommonBot.ProfileManager.Load(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\BWTmp.xml");
            BotMain.Start();
        }

        #region Move Buddy Tab
        StringBuilder moveBuddySb = new StringBuilder();
        public void ClearPositionLog()
        {
            moveBuddySb.Clear();
            moveBuddyXml.Text = "";
        }
        public void TrackPosition(Vector3 v)
        {
            if (!enableTrackingChk.Checked) return;

            MoveToElement mTo = new MoveToElement(v);
            moveBuddySb.AppendLine("\t\t" + mTo.ToString());
            moveBuddyXml.Text = "<Profile>\r\n\t<Order>\r\n" + moveBuddySb.ToString() + "\t</Order>\r\n</Profile>";
            moveBuddyXml.FormatXml();
            moveBuddyXml.RestoreCaret(moveBuddyXml.Text.Length);
        }
        private void enableTrackingChk_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void enableUnstuckChk_CheckedChanged(object sender, EventArgs e)
        {
            BossHogPlugin.EnableUnstucker = enableUnstuckChk.Checked;
        }
        #endregion
        #region Actors Tab
        void actorGrid_DataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }
        private void rebindActorsBtn_Click(object sender, EventArgs e)
        {
            BindActors();
        }
        #endregion

        void NewProfileTab()
        {
            TabPage t = new TabPage();
            t.Text = "New Tab";
            ProfileEditor xml = new ProfileEditor();
            t.Controls.Add(xml);
            xml.Dock = DockStyle.Fill;
            
            profileTabs.TabPages.Add(t);
            profileTabs.Update();
            t.Update();
            xml.FormatXml();

        }

        private void newProfilePage_Click(object sender, EventArgs e)
        {
            NewProfileTab();
        }
    }
}
